package com.lingyu.jsjx.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.lingyu.jsjx.domain.UserDocument;
import com.lingyu.jsjx.service.IUserDocumentService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.lingyu.jsjx.domain.DocumentShare;
import com.lingyu.jsjx.service.IDocumentShareService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 共享
 * Controller
 *
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/documentShare")
public class DocumentShareController extends BaseController {
    @Autowired
    private IDocumentShareService documentShareService;

    @Autowired
    private IUserDocumentService userDocumentService;

    /**
     * 新增共享
     */
    @RequiresPermissions("jsjx:documentShare:add")
    @Log(title = "共享", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DocumentShare documentShare) {
        String[] deptIds = documentShare.getDeptId().split(",");//组织机构
        List<DocumentShare> shareList = new ArrayList<>();
        UserDocument userDocument = userDocumentService.selectUserDocumentByWordId(documentShare.getWordId());
        userDocument.setAllowShare(1);//设置共享状态
        userDocument.setShareTime(DateUtils.getNowDate()); //设置共享时间
        userDocumentService.updateUserDocument(userDocument);
        for (String deptId : deptIds) {
            DocumentShare ds = new DocumentShare();
            ds.setShareId(null);
            ds.setWordId(documentShare.getWordId());
            ds.setDeptId(deptId);
            ds.setCreateBy(SecurityUtils.getUsername());
            ds.setCreateTime(DateUtils.getNowDate());
            shareList.add(ds);
        }

        return toAjax(documentShareService.batchInsertDocumentShare(shareList));
    }


    /**
     * 删除共享
     */
    @RequiresPermissions("jsjx:documentShare:remove")
    @Log(title = "共享", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wordId}")
    public AjaxResult remove(@PathVariable("wordId") Long wordId) {
        UserDocument userDocument = userDocumentService.selectUserDocumentByWordId(wordId);
        userDocument.setAllowShare(0);
        userDocument.setShareTime(null);
        userDocumentService.updateUserDocument(userDocument);
        Long[] shareIds = documentShareService.selectDocumentShareByWordId(wordId).stream().map(DocumentShare::getShareId).toArray(Long[]::new);
        return toAjax(documentShareService.deleteDocumentShareByShareIds(shareIds));
    }
}
